package net.wanho.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.wanho.common.Constants;
import net.wanho.dto.UserDTO;
import net.wanho.exception.ServiceException;
import net.wanho.po.User;
import net.wanho.mapper.UserMapper;
import net.wanho.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-12-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public void register(UserDTO userDTO) {
        /**
         * 判断用户是否已存在
         */
        User user = getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, userDTO.getUsername()));
        if (user != null){
            throw new ServiceException("用户名已存在");
        }

        // 将UserDTO转换为User对象
        user = new User();
        // user.setUsername(userDTO.getUsername());
        // user.setPassword(userDTO.getPassword());
        BeanUtils.copyProperties(userDTO, user);

        user.setNickname(user.getUsername());
        user.setRole(Constants.ROLE_USER);

        save(user);
    }
}
